const {
	http
} = require("../../../../utils/apiutils")
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isCanDraw: false,
		pageNo: 0,
		total: 0,
		dataList: [],
		tabActive: 1,
		hasMore: true,
		loading: false,
		content: null,
		code_img: '',
		bg_img: 'https://linghuoimage.xbbzp.com/Fk9oDGuLhXnLFmey0oZOZmDmeME4',
		obj: {
			width:"750",
			height:"2984",
			left:"275",
			bottom:"210",
			code_width:200,
			code_height:200,
			btn_background:'#147277'
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */

	onLoad: function (options) {
		this.setData({
			nickName: wx.getStorageSync('nickName') || '',
			avatarUrl: wx.getStorageSync('avatarUrl') || ''
		})
		this.getinvite();
		let _this = this;
	},
	createShareImage() {
		this.setData({
			isCanDraw: !this.data.isCanDraw
		})
	},
	onclickCall1: function (e) {
		let mo = e.currentTarget.dataset.phone;
		wx.makePhoneCall({
			phoneNumber: mo,
		})

	},

	ages: function (str) {
		var r = str.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
		console.log("-----", r);
		if (r == null) return "未知";
		var d = new Date(r[1], r[2] - 1, r[3]);
		console.log("-----------", d);
		if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d.getDate() == r[3]) {
			var D = new Date();
			var Y = D.getFullYear();
			var M = D.getMonth() - 1;
			var nf = Y - r[1];
			var yf = 0
			if (M - r[2] > 0) {
				yf = M - r[2];
			} else {
				nf = nf - 1;
				yf = M + 12 - r[2];
			}
			return (nf + "岁" + yf + "个月");
		}
		return "未知";
	},

	getinvite() {
		var data = {
			"pageNo": this.data.pageNo + 1, //手机号码
			"pageSize": "10", //密码
			type: this.data.tabActive
		}
		if (this.data.loading) {
			return false;
		}
		if (this.data.loading) return false;
		if (!this.data.hasMore) {
			wx.showToast({
				title: '没有更多数据',
				icon: "none"
			})
			return false;
		}
		this.data.loading = true;
		var that = this;
		wx.showLoading({
			title: '加载中...',
		})
		http.extend(this.data.tabActive,
			(res) => {
				wx.hideLoading()
				console.log("------s收藏岗位列表---", res);
				if (res.code == 0) {
					var pageNo = res.data.current;
					var total = res.data.pages;
					var arr = res.data;
					if (arr.length > 0) {
						arr.forEach(function (item) {
							item.nl = item.age
						});
					}
					that.setData({
						dataList: arr,
						pageNo,
						total
					})
					if (pageNo == 1) {
						that.setData({
							dataList: arr,
							pageNo,
							total
						})
					} else if (pageNo <= total) {
						let hasMore = true;
						if (pageNo == total) hasMore = false;
						that.setData({
							dataList: [...that.data.dataList, ...arr],
							pageNo,
							total,
							hasMore
						})
					}
				} else {
					wx.showToast({
						title: res.message,
						icon: "none",
					})
				}
				this.data.loading = false;
			},
			(err) => {
				wx.hideLoading()
			})

	},
	tabChange(event) {
		let title = event.detail.title;
		console.log(event)
		this.setData({
			pageNo: 0,
			total: 0,
			dataList: [],
			tabActive: event.detail.name
		})
		wx.nextTick(() => {
			this.getinvite()
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		var shareObj = {
			title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
			path: '/pages/index/index?rId=' + getApp().globalData.userInfo.id, // 
			imageUrl: '',
			success: function (res) {
				// 转发成功之后的回调
				if (res.errMsg == 'shareAppMessage:ok') { }
			}
		}
		console.log("------------", shareObj);
		// 返回shareObj
		return shareObj;
	},
	getImgCode(url) {
	},
	getShareCode() {
		wx.showLoading()
		let params = {
			rId: getApp().globalData.userInfo.id + '_1'
		}
		http.getShareCode(params, res => {
			wx.hideLoading({})
			let src = 'data:image/jpeg;base64,' + res;
			this.setData({
				code_img: src
			})
			this.createShareImage();
		}, err => {
			wx.hideLoading({})
			wx.showToast({
				title: '请重新点击',
				icon: "none"
			})
		})
	}
})
const fsm = wx.getFileSystemManager();
const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名

function base64src(base64data, cb) {
	const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
	if (!format) {
		return (new Error('ERROR_BASE64SRC_PARSE'));
	}
	const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
	const buffer = wx.base64ToArrayBuffer(bodyData);
	fsm.writeFile({
		filePath,
		data: buffer,
		encoding: 'binary',
		success() {
			cb(filePath);
		},
		fail() {
			return (new Error('ERROR_BASE64SRC_WRITE'));
		},
	});
};
